package com.sf.wisdomkg.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.bumptech.glide.Glide;
import com.sf.wisdomkg.R;
import com.sf.wisdomkg.utils.CommonUtils;
import com.sf.wisdomkg.utils.MyOnItemClickListener;
import com.sf.wisdomkg.utils.MyOnItemLongClickListener;
import com.sf.wisdomkg.xRefreshView.DensityUtil;

import java.util.ArrayList;
import java.util.HashMap;

public class KaoQinListAdapter extends BaseRecyclerAdapter<KaoQinListAdapter.KaoQinListAdapterHolder> {
    private ArrayList<HashMap<String, String>> arrayList;
    private int largeCardHeight, smallCardHeight;
    private Context context;
    //点击事件的定义
    private MyOnItemClickListener itemClickListener;
    private MyOnItemLongClickListener itemLongClickListener;

    public KaoQinListAdapter(ArrayList<HashMap<String, String>> list, Context context) {
        this.arrayList = list;
        largeCardHeight = DensityUtil.dip2px(context, 150);
        smallCardHeight = DensityUtil.dip2px(context, 100);
        this.context = context;
    }

    @Override
    public void onBindViewHolder(final KaoQinListAdapterHolder holder, int position, boolean isItem) {
        holder.kaoQin_name.setText(arrayList.get(position).get("studentName"));
        if (arrayList.get(position).get("sex").equals("0")){

            holder.head_img.setImageResource(R.mipmap.boy_head_icon);
        }else {

            holder.head_img.setImageResource(R.mipmap.girl_head_icon);
        }
        if (arrayList.get(position).get("classType").equals("上课")){
            holder.kaoQin_type.setImageResource(R.mipmap.up_class_icon);
            holder.kaoqin_time.setText(arrayList.get(position).get("startTime"));

        }else {
            holder.kaoQin_type.setImageResource(R.mipmap.down_class_icon);
            holder.kaoqin_time.setText(arrayList.get(position).get("endTime"));
        }
        System.out.println("路径"+CommonUtils.IP_S+arrayList.get(position).get("img"));
        Glide.with(context).load(CommonUtils.IP_S+arrayList.get(position).get("img")).into(holder.kaoQIn_img);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();


        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListener.OnItemClickListener(holder.itemView, holder.getLayoutPosition());
                }
            });
        } /*自定义item的长按事件不为null，设置监听事件*/
        if (itemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    itemLongClickListener.OnItemLongClickListener(holder.itemView, holder.getLayoutPosition());
                    return true;
                }
            });
        }

        if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
            holder.rootView.getLayoutParams().height = position % 2 != 0 ? largeCardHeight : smallCardHeight;
        }
    }

    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return arrayList.size();
    }

    @Override
    public KaoQinListAdapterHolder getViewHolder(View view) {
        return new KaoQinListAdapterHolder(view, false);
    }

    public void setData(ArrayList<HashMap<String, String>> list) {
        this.arrayList = list;
        notifyDataSetChanged();
    }

    @Override
    public KaoQinListAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.kaoqin_list_item, parent, false);
        KaoQinListAdapterHolder vh = new KaoQinListAdapterHolder(v, true);
        return vh;
    }

    public void remove(int position) {
        remove(arrayList, position);
    }

    public void clear() {
        clear(arrayList);
    }

    public class KaoQinListAdapterHolder extends RecyclerView.ViewHolder {

        public View rootView;
        public TextView kaoQin_name;
        public ImageView kaoQin_type;
        public TextView kaoqin_time;
        public ImageView kaoQIn_img;
        public ImageView head_img;
        public KaoQinListAdapterHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                kaoQin_name = (TextView) itemView.findViewById(R.id.kaoQin_name);
                kaoQin_type = (ImageView) itemView.findViewById(R.id.kaoQin_type);
                kaoqin_time = (TextView) itemView.findViewById(R.id.kaoqin_time);
                kaoQIn_img = (ImageView) itemView.findViewById(R.id.kaoQIn_img);
                head_img = (ImageView)itemView.findViewById(R.id.head_img);

            }

        }
    }

    /**
     * 列表点击事件
     *
     * @param itemClickListener
     */
    public void setOnItemClickListener(MyOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 列表长按事件
     *
     * @param itemLongClickListener
     */
    public void setOnItemLongClickListener(MyOnItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }
}